package com.wzt.lianfirecontrol.fragment.xuncha;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.adapter.DateHistoryAdapter;
import com.wzt.lianfirecontrol.adapter.xuncha.XunChaListAdapter;
import com.wzt.lianfirecontrol.adapter.xuncha.devicexuncha.XunChaDevicePlanAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.DateModel;
import com.wzt.lianfirecontrol.bean.recode.ListModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.XunChaPlanModel;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.devicexuncha.XunChaDevicePlanModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.inter.SetTime;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public class XunChaHistoryFragment extends BaseFragment implements SetTime {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private RecyclerView rlv_week_list;
    private LinearLayoutManager llm_week;
    private DateHistoryAdapter dateHistoryAdapter;
    private List<DateModel> dateModelList = new ArrayList<>();

    private LinearLayout ll_info_list;
    private RecyclerView rlv_info_list;
    private XunChaListAdapter baseRecyclerAdapter;
    private LinearLayoutManager llm_info_list;

    private LinearLayout ll_jiancha_list;
    private RecyclerView rlv_jiancha_list;
    private XunChaListAdapter jianchaAdapter;
    private LinearLayoutManager llm_jiancha_list;

    private LinearLayout ll_device_list;
    private RecyclerView rlv_device_list;
    private XunChaDevicePlanAdapter deviceAdapter;
    private LinearLayoutManager llm_device_list;

    private View include_no_net;
    private View include_progress_bar;
    private View include_no_data;

    private static final int INFO_LIST_REQUEST_WHAT = -1;
    private static final int JIANCHA_LIST_REQUEST_WHAT = -2;
    private static final int DEVICE_LIST_REQUEST_WHAT = -3;

    private Dialog datePickerDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_xuncha_history, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {

        initWeekHeadView();

        initListView();

        initJianChaListView();

        initDeviceListView();

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_no_data.setVisibility(View.GONE);
        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
        // 切换日历
        if (activity instanceof ShowFragActivity){
            ((ShowFragActivity)activity).setImageEtAction(R.mipmap.icon_calendar_white, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 选择时间
                    if (null != dateHistoryAdapter) {
                        createDatePickerDialog(dateHistoryAdapter.getCurrentDataStr());
                    }
                }
            });
        }
    }

    private void initWeekHeadView() {
        rlv_week_list = itemContentView.findViewById(R.id.rlv_week_list);
        llm_week = new LinearLayoutManager(activity);
        llm_week.setOrientation(RecyclerView.HORIZONTAL);
        rlv_week_list.setLayoutManager(llm_week);
        dateHistoryAdapter = new DateHistoryAdapter(activity);
        rlv_week_list.setAdapter(dateHistoryAdapter);
        DateModel dateModel = new DateModel();
        dateModelList.add(dateModel);
        DateModel dateModel1 = new DateModel();
        dateModelList.add(dateModel1);
        DateModel dateModel2 = new DateModel();
        dateModelList.add(dateModel2);
        DateModel dateModel3 = new DateModel();
        dateModelList.add(dateModel3);
        DateModel dateModel4 = new DateModel();
        dateModelList.add(dateModel4);
        DateModel dateModel5 = new DateModel();
        dateModelList.add(dateModel5);
        DateModel dateModel6 = new DateModel();
        dateModelList.add(dateModel6);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        setCalendarView(sdf.format(new Date()));

        dateHistoryAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<DateModel>() {
            @Override
            public void onItemClick(View v, int position, DateModel data) {
                dateHistoryAdapter.setCurrentDataStr(data.getDateStr());
                dateHistoryAdapter.notifyDataSetChanged();
                updateData();
            }
        });
    }

    private void initListView() {
        ll_info_list = itemContentView.findViewById(R.id.ll_info_list);
        rlv_info_list = itemContentView.findViewById(R.id.rlv_info_list);
        llm_info_list = new LinearLayoutManager(activity);
        llm_info_list.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_info_list.setLayoutManager(llm_info_list);

        baseRecyclerAdapter = new XunChaListAdapter(activity);
        baseRecyclerAdapter.setHistory(true);
        rlv_info_list.setAdapter(baseRecyclerAdapter);
        baseRecyclerAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<XunChaPlanModel>() {
            @Override
            public void onItemClick(View v, int position, XunChaPlanModel data) {
                // 防火巡查检查列表点击动作
                Bundle skipToBundle = new Bundle();
                skipToBundle.putSerializable("data", data);
                BannerModel bannerModel = new BannerModel();
                bannerModel.setActionType(ConstData.SHOWFRAG);
                if ("2".equals(data.getStatus())){
                    // 已完成
                    bannerModel.setActionValue(ConstData.XUNCHACOMPLETE);
                    bannerModel.setActionTitle( "巡查详情");
                }else {
                    bannerModel.setActionValue(ConstData.STARTXUNCHA);
                    bannerModel.setActionTitle("巡查详情");
                }
                activity.setClickAction(bannerModel, skipToBundle);
            }
        });
    }

    private void initJianChaListView() {
        ll_jiancha_list = itemContentView.findViewById(R.id.ll_jiancha_list);
        rlv_jiancha_list = itemContentView.findViewById(R.id.rlv_jiancha_list);
        llm_jiancha_list = new LinearLayoutManager(activity);
        llm_jiancha_list.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_jiancha_list.setLayoutManager(llm_jiancha_list);

        jianchaAdapter = new XunChaListAdapter(activity);
        jianchaAdapter.setHistory(true);
        rlv_jiancha_list.setAdapter(jianchaAdapter);
        jianchaAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<XunChaPlanModel>() {
            @Override
            public void onItemClick(View v, int position, XunChaPlanModel data) {
                // 防火巡查检查列表点击动作
                Bundle skipToBundle = new Bundle();
                skipToBundle.putSerializable("data", data);
                BannerModel bannerModel = new BannerModel();
                bannerModel.setActionType(ConstData.SHOWFRAG);
                if ("2".equals(data.getStatus())){
                    // 已完成
                    bannerModel.setActionValue(ConstData.XUNCHACOMPLETE);
                    bannerModel.setActionTitle( "巡查详情");
                }else {
                    bannerModel.setActionValue(ConstData.STARTXUNCHA);
                    bannerModel.setActionTitle("巡查详情");
                }
                activity.setClickAction(bannerModel, skipToBundle);
            }
        });
    }


    private void initDeviceListView() {
        ll_device_list = itemContentView.findViewById(R.id.ll_device_list);
        rlv_device_list = itemContentView.findViewById(R.id.rlv_device_list);
        llm_device_list = new LinearLayoutManager(activity);
        llm_device_list.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_device_list.setLayoutManager(llm_device_list);

        deviceAdapter = new XunChaDevicePlanAdapter(activity);
        deviceAdapter.setHistory(true);
        rlv_device_list.setAdapter(deviceAdapter);
    }

    private void initHttpHelper(){
        initInfoHttpHelper();
        initJianChaHttpHelper();
        initDeviceHttpHelper();
    }

    /**
     * 获取列表
     */
    private void initInfoHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(ConstData.COMPANYID, UserInfoModel.getCompandId(activity));
        hashMap.put("type", "1");
        hashMap.put("startDate", dateHistoryAdapter.getCurrentDataStr());
        hashMap.put("endDate", dateHistoryAdapter.getCurrentDataStr());
        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XUNCHA_LIST_URL,
                INFO_LIST_REQUEST_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }

    /**
     * 获取列表
     */
    private void initJianChaHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(ConstData.COMPANYID, UserInfoModel.getCompandId(activity));
        hashMap.put("type", "2");
        hashMap.put("startDate", dateHistoryAdapter.getCurrentDataStr());
        hashMap.put("endDate", dateHistoryAdapter.getCurrentDataStr());
        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XUNCHA_LIST_URL,
                JIANCHA_LIST_REQUEST_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }

    /**
     * 获取列表
     */
    private void initDeviceHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(ConstData.COMPANYID, UserInfoModel.getCompandId(activity));
        hashMap.put("startDate", dateHistoryAdapter.getCurrentDataStr());
        hashMap.put("endDate", dateHistoryAdapter.getCurrentDataStr());
        HashMap<String, String> url_params = new HashMap<>();
        url_params.put(ConstData.JSONOBJECT, JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XUNCHA_DEVICE_BUILDING_URL,
                DEVICE_LIST_REQUEST_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
    }


    private class InfoListListHttpHelper extends HttpHelper{
        public InfoListListHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }
    }

    protected class InfoListParseJsonData extends ParseJsonData {

        public InfoListParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            try {
                JSONObject jsonObject = new JSONObject(dataStr);
                switch (what){
                    case INFO_LIST_REQUEST_WHAT:
                    case JIANCHA_LIST_REQUEST_WHAT:

                        LinkedList<XunChaPlanModel> xunChaListModels = (LinkedList<XunChaPlanModel>) JSONUtil.jsonArrayToBeanList(
                                new JSONArray(jsonObject.getString("list")), XunChaPlanModel.class);
                        bundle.putSerializable("list", xunChaListModels);
                        bundle.putBoolean(REQUEST_OK, true);
                        break;
                    case DEVICE_LIST_REQUEST_WHAT:
                        ListModel listModel = JSONUtil.jsonObjectToBean(jsonObject, ListModel.class);
                        if (jsonObject.has("list")) {
                            LinkedList<XunChaDevicePlanModel> xunChaDevicePlanModels = (LinkedList<XunChaDevicePlanModel>) JSONUtil.jsonArrayToBeanList(
                                    new JSONArray(jsonObject.getString("list")), XunChaDevicePlanModel.class);
                            bundle.putSerializable("list", xunChaDevicePlanModels);
                            bundle.putBoolean(REQUEST_OK, true);

                            break;
                        }
                }
            } catch (Exception e) {
                e.printStackTrace();
                bundle.putBoolean(REQUEST_OK, false);
                bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            //ToastUtils.showToast(activity, bundle.getString(ParseJsonData.ERROR_MSG));
            if (msg.what == INFO_LIST_REQUEST_WHAT || msg.what == JIANCHA_LIST_REQUEST_WHAT || msg.what == DEVICE_LIST_REQUEST_WHAT) {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }
        switch (msg.what){
            case INFO_LIST_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                setListData((LinkedList<XunChaPlanModel>)bundle.getSerializable("list"));
                break;
            case JIANCHA_LIST_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                setJianChaListData((LinkedList<XunChaPlanModel>)bundle.getSerializable("list"));
                break;
            case DEVICE_LIST_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                setDeviceListData((LinkedList<XunChaDevicePlanModel>)bundle.getSerializable("list"));
                break;
        }
    }

    private void setListData(LinkedList<XunChaPlanModel> list) {
        if (null == list || list.size() == 0){
            baseRecyclerAdapter.clear();
            ll_info_list.setVisibility(View.GONE);
            if (ll_info_list.getVisibility() == View.GONE && ll_jiancha_list.getVisibility() == View.GONE && ll_device_list.getVisibility() == View.GONE){
                include_no_data.setVisibility(View.VISIBLE);
            }
            return;
        }
        include_no_data.setVisibility(View.GONE);
        ll_info_list.setVisibility(View.VISIBLE);
        baseRecyclerAdapter.clear();
        baseRecyclerAdapter.addDatas(list);
    }

    private void setJianChaListData(LinkedList<XunChaPlanModel> list) {
        if (null == list || list.size() == 0){
            jianchaAdapter.clear();
            ll_jiancha_list.setVisibility(View.GONE);
            if (ll_info_list.getVisibility() == View.GONE && ll_jiancha_list.getVisibility() == View.GONE && ll_device_list.getVisibility() == View.GONE){
                include_no_data.setVisibility(View.VISIBLE);
            }
            return;
        }
        ll_jiancha_list.setVisibility(View.VISIBLE);
        include_no_data.setVisibility(View.GONE);
        jianchaAdapter.clear();
        jianchaAdapter.addDatas(list);
    }

    private void setDeviceListData(LinkedList<XunChaDevicePlanModel> list) {
        if (null == list || list.size() == 0){
            deviceAdapter.clear();
            ll_device_list.setVisibility(View.GONE);
            if (ll_info_list.getVisibility() == View.GONE && ll_jiancha_list.getVisibility() == View.GONE && ll_device_list.getVisibility() == View.GONE){
                include_no_data.setVisibility(View.VISIBLE);
            }
            return;
        }
        include_no_data.setVisibility(View.GONE);
        ll_device_list.setVisibility(View.VISIBLE);
        deviceAdapter.clear();
        deviceAdapter.addDatas(list);
    }

    @Override
    public void updateData() {
        super.updateData();
        initHttpHelper();
    }

    private void setCalendarView(String dateStr){
        if (StringUtils.isEmpty(dateStr)){
            return;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date = sdf.parse(dateStr);
            int week = getDayofWeek(dateStr);

            switch (week){
                case 1:// 星期天
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, 1));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, 2));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, 3));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, 4));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 5));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 6));
                    break;
                case 2:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, 1));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, 2));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, 3));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 4));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 5));
                    break;
                case 3:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -2));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, 1));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, 2));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 3));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 4));
                    break;
                case 4:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -3));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, -2));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, 1));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 2));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 3));
                    break;
                case 5:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -4));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, -3));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, -2));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 1));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 2));
                    break;
                case 6:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -5));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, -4));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, -3));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, -2));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, 0));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 1));
                    break;
                case 7:
                    dateModelList.get(0).setDateStr(getTimeDateStr(date, -6));
                    dateModelList.get(1).setDateStr(getTimeDateStr(date, -5));
                    dateModelList.get(2).setDateStr(getTimeDateStr(date, -4));
                    dateModelList.get(3).setDateStr(getTimeDateStr(date, -3));
                    dateModelList.get(4).setDateStr(getTimeDateStr(date, -2));
                    dateModelList.get(5).setDateStr(getTimeDateStr(date, -1));
                    dateModelList.get(6).setDateStr(getTimeDateStr(date, 0));
                    break;
            }
            dateHistoryAdapter.clear();
            dateHistoryAdapter.setCurrentDataStr(getTimeDateStr(date, 0));
            dateHistoryAdapter.addDatas(dateModelList);
        }catch (Exception e){}
    }

    private int getDayofWeek(String dateTime) {
        java.util.Calendar cal = java.util.Calendar.getInstance();
        if (dateTime.equals("")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date date;
            try {
                date = sdf.parse(dateTime);
            } catch (ParseException e) {
                date = null;
                e.printStackTrace();
            }
            if (date != null) {
                cal.setTime(new Date(date.getTime()));
            }
        }
        return cal.get(java.util.Calendar.DAY_OF_WEEK);
    }

    private String getTimeDateStr(Date date, int interval){
        long mills = date.getTime() + interval * 24 * 60 *60 * 1000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD);
        Date nextDate = new Date(mills);
        return simpleDateFormat.format(nextDate);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DialogUtils.closeDialog(datePickerDialog);
    }

    /**
     * 时间选择器
     */
    public void createDatePickerDialog(String time){
        datePickerDialog = DialogUtils.createDatePickerDialog(activity, time, this);
        DialogUtils.showDialog(datePickerDialog);
    }

    @Override
    public void setFilterTime(int year, int month, int day) {
        if (null != dateHistoryAdapter){
            String monthStr;
            if (month <= 9){
                monthStr = "0" + month;
            }else {
                monthStr = "" + month;
            }
            String dayStr;
            if (day <= 9){
                dayStr = "0" + day;
            }else {
                dayStr = "" + day;
            }
            setCalendarView(year + "-" + monthStr + "-" + dayStr);
            updateData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateData();
    }
}
